var main = {};
main.canClick = true;

(function ($) {
    main.init = function () {
        var self = this;

        self.initEvent();
        //self.initSwiper();
    };

    main.initEvent = function () {
        var self = this;

        /**
         * @desc 点击转盘旋转
         * num为奖项类别
         * 0：再试一次
         * 1：谢谢参与
         * 2：1:1机器人
         * 3：电影票
         * 4：手办
         */
        $("#btn").on("click", function () {
            if (main.canClick) {
                main.canClick = false;
                var total = $("#number").html();
                if(total!=0){
                	$("#number").html(parseInt(total)-1);
            	}
                /*随机生成0-4*/
                /**
                 * 这里改成向后台发请求
                 */
                //var num = Math.floor(Math.random() * 6);

              //是否可以再点击抽奖
                    $.ajax({
                    	url:$("#path").val(),
                    	type: 'POST',
                    	data:{
                    	},
                    	success: function (data) {
                    		if(data.code==100){
                    			main.showDialog("系统提示",'<p style="font-weight: bold;text-align: center;color:#f00">很遗憾您没有抽奖机会了' +
                			            '<br></p></p>' +
                			            '<div class="textCenter"> </div> ','确定',function(){
                					//todo  回跳路径
                					//window.location.href=path+"/manage/order";
                				});
                    			main.canClick=true;
                    			return ;
                    		}
                    		if(data.code==999){
                    			main.showDialog("系统提示",'<p style="font-weight: bold;text-align: center;color:#f00">活动已关闭' +
                			            '<br></p></p>' +
                			            '<div class="textCenter"> </div> ','确定',function(){
                					//todo  回跳路径
                					//window.location.href=path+"/manage/order";
                				});
                    			main.canClick=true;
                    			return ;
                    		}
                    		if(data.code==500){
                    			var num=data.result.level;
                    			self.rotate(num, function () {
                    				main.showDialog("中奖了","<p style='font-weight: bold;text-align: center;color:#f00'>恭喜您抽中了"+data.result.name+"</p>","确定",function(){
        								//window.location=data.result.coupon.url;
                    					//todo  回跳路径
                    					//window.location.href=path+"/manage/order";
        							})
                    				main.canClick = true;
                    			});
                    			
                    		}else{
                    			var num = 4;
                    			self.rotate(num, function () {
                    				main.showDialog("谢谢参与",'<p style="font-weight: bold;text-align: center;color:#f00">很遗憾您没有中将奖' +
                    			            '<br>请再接再厉</p>更多优惠互动敬请期待！</p>' +
                    			            '<div class="textCenter"> </div> ','确定',function(){
                    					//todo  回跳路径
        								//window.location.href=path+"/manage/order";
                    				});
                    				main.canClick = true;
                    			});
                    		}
                    	},
                    	error: function (xhr, errorInfo, ex){
                    		
                    	}
                    });
                /**end*/
            }
        });
    };

    /**
     * @func rotate
     * @desc 控制转盘旋转
     * @param callback
     */
    main.rotate = function (num, callback) {
        var dial = $("#dial");

        dial.removeClass("rotate");
        dial.css({
            "transform": "rotate(0deg)"
        });

        setTimeout(function () {
            var deg = 1800 + 36 + num * 72;
            dial.addClass("rotate");
            dial.css({
                "transform": "rotate(" + deg + "deg)"
            });
        }, 30);

        setTimeout(function () {
            callback();
        }, 2500);
    };

    /*弹框*/
    main.showDialog = function (title, content, callback) {
        var c = callback || function () {
            };

        $(".mask").show();
        $(".alert1").show();
        $("#title").html(title);
        $("#content").html(content);
        setTimeout(function () {
            $(".mask").css("opacity", 1);
            $(".alert1").css("opacity", 1);
        }, 30);

        $(".close").on("click", function () {
            $(".mask").hide();
            $(".alert1").hide();
        })
    };

    /*关闭弹框*/
    main.closeDialog = function () {
        $(".mask").hide();
        $(".alert1").hide();
    };

    /*显示表单*/
    main.showForm = function (callback) {
        var c = callback || function () {
            };

        $(".mask").show();
        $(".alert2").show();
        setTimeout(function () {
            $(".mask").css("opacity", 1);
            $(".alert2").css("opacity", 1);
        }, 30);

        $(".close").on("click", function () {
            c();
        })
    };

    /*关闭弹框*/
    main.closeForm = function () {
        $(".mask").hide();
        $(".alert2").hide();
    };

    main.initSwiper = function () {
        var mySwiper = new Swiper ('.swiper-container', {
            direction: 'vertical',
            loop: true,
            slidesPerView: 1,
            autoplay: 1500,
            autoplayDisableOnInteraction: false
        })
    }
})(jQuery);

main.init();